package com.xuanwei.core.courier;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xuanwei.core.entity.CourierInfo;
import com.xuanwei.core.enums.CourierStatus;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/8/8 18:16
 **/
public class ZTQuery extends QueryCourier {

    @Override
    public CourierInfo query(String billCode) throws IOException {
        Document document = Jsoup.connect("https://hdgateway.zto.com/WayBill_GetDetail")
                .data("billCode", billCode)
                .ignoreContentType(true)
                .post();
        String json = document.body().wholeText();
        if (StringUtils.isNotBlank(json)) {
            JSONObject object = JSON.parseObject(json);
            CourierInfo courierInfo = new CourierInfo();
            JSONArray logisticsRecord = object.getJSONObject("result").getJSONArray("logisticsRecord");
            List<CourierInfo.Info> infos = new ArrayList<>();
            for (int i = 0; i < logisticsRecord.size(); i++) {
                JSONArray jsonArray = logisticsRecord.getJSONArray(i);
                for (int j = 0; j < jsonArray.size(); j++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(j);
                    if (i == 0 && j == 0) {
                        String scanType = jsonObject.getString("scanType");
                        courierInfo.setStatus(getStatus(scanType));
                    }
                    String stateDescription = jsonObject.getString("stateDescription");
                    String scanDate = jsonObject.getString("scanDate");

                    CourierInfo.Info info = new CourierInfo.Info();
                    info.setTime(scanDate);
                    info.setMessage(stateDescription);
                    infos.add(info);
                }
            }
            courierInfo.setInfos(infos);
            return courierInfo;
        }

        return null;
    }

    private Integer getStatus(String scanType) {

        Integer status;

        switch (scanType) {
            case "SIGNED":
                status = CourierStatus.SUCCESS.getCode();
                break;
            case "ARRIVAL":
                status = CourierStatus.DELIVERY.getCode();
                break;
            case "派件":
                status = CourierStatus.DELIVERY.getCode();
                break;
            case "到件":
                status = CourierStatus.TRANSPORT.getCode();
                break;
            case "发件":
                status = CourierStatus.TRANSPORT.getCode();
                break;
            case "收件":
                status = CourierStatus.PACKAGE.getCode();
                break;
            default:
                status = CourierStatus.EXCEPTION.getCode();
                break;
        }

        return status;
    }

}
